package com.netty.client;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.concurrent.TimeUnit;

/**
 * 
 * SS连接事件
 * 
 * @author zhoufang
 *
 *         2018年1月9日 上午10:43:37
 */
public class NettyConnectListener implements
		GenericFutureListener<ChannelFuture> {

	private ClientNettyConnect clientConnect;

	public NettyConnectListener(ClientNettyConnect clientConnect) {
		this.clientConnect = clientConnect;
	}

	@Override
	public void operationComplete(ChannelFuture future) throws Exception {
		Channel channel = future.channel();
		if (future.isSuccess()) {
			clientConnect.addChannel(channel);
		} else {
			// 启动重连
			System.out.println("serverId=" + clientConnect.getServerId()
					+ ", 连接失败,5秒后重连");
			channel.eventLoop().schedule(() -> clientConnect.connect(), 5,
					TimeUnit.SECONDS);
		}
	}

}
